function subPlotIndex=multiDraw(varargin)
%NOTE: please use together with multiDrawEnd()
%this function is used to plot multi subplots on on panel
%multiDrawEnd() will reset the global parameter
%'Global_multiDrawFunctionSet_figure'
%Version beta 1.0  2015/6/22

%example: subPlotIndex=multiDraw([2,1],'title','code Comparison','colorbar',[0,0]); 
%example: multiDrawEnd();
screenSize=[1920,1080];
global Global_multiDrawFunctionSet_figure;


if length(varargin)<1
    error('not enough input argument'); 
elseif mod(length(varargin),2)==0
    error('input argument number must be odd');
end

titleText='';
xLabelText='';
yLabelText='';
gap=0;
figureS=[0 0];
figureInd=1;
subplotSize=[0 0];
note='';
SideSqueeze=[0 0];
maxSizeSwitch=1;
xticklabelSwitch=1;
yticklabelSwitch=1;

%inteperte varargin-----------------------------------
argumentN=(length(varargin)-1)/2;
for i=1:argumentN
 
    if strcmp( varargin{i*2},'title')
        titleText=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'xLabel')
        xLabelText=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'yLabel')
        yLabelText=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'xticklabel')
        if strcmp(varargin{i*2+1},'off')
            xticklabelSwitch=0;
        elseif strcmp(varargin{i*2+1},'on')
            xticklabelSwitch=1;
        else
            error('unknown xticklabel property');
        end
    elseif strcmp( varargin{i*2},'yticklabel')
        if strcmp(varargin{i*2+1},'off')
            yticklabelSwitch=0;
        elseif strcmp(varargin{i*2+1},'on')
            yticklabelSwitch=1;
        else
            error('unknown yticklabel property');
        end
    elseif strcmp( varargin{i*2},'gap')
        gap=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'figureSize')
        figureS=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'figureInd')
        figureInd=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'subplotSize')
        subplotSize=varargin{i*2+1};
    elseif strcmp( varargin{i*2},'maxSize')
        if strcmp(varargin{i*2+1},'off')
            maxSizeSwitch=0;
        elseif strcmp(varargin{i*2+1},'on')
            maxSizeSwitch=1;
        else
            error('unknown maxSize property');
        end
    elseif strcmp( varargin{i*2},'note')
        note=varargin{i*2+1};
        SideSqueeze(2)=SideSqueeze(2)+0.15;
    elseif strcmp( varargin{i*2},'colorbar')
        SideSqueeze(1)=SideSqueeze(1)+0.02;
    elseif strcmp( varargin{i*2},'colormap')
    else
        error('input argument type unknown');
    end
end

%global parameters -------------------------------------------------
Global_multiDrawFunctionSet_figure(figureInd).varargin=varargin;
Global_multiDrawFunctionSet_figure(figureInd).tickSwitch=0;
Global_multiDrawFunctionSet_figure(figureInd).subPlotIndex=0;
Global_multiDrawFunctionSet_figure(figureInd).SideSqueeze=SideSqueeze;
%-------------------------------------------------------------------




subPlotMatrix=varargin{1};
width=subPlotMatrix(1);
height=subPlotMatrix(2);

xPlotRange=[0.1 0.9-SideSqueeze(1)];
yPlotRange=[0.1+SideSqueeze(2) 0.9];
xPlotRangeStep=(xPlotRange(2)-xPlotRange(1))/width-gap;
yPlotRangeStep=(yPlotRange(2)-yPlotRange(1))/height-gap;


%tick switch [x,y];
Global_multiDrawFunctionSet_figure(figureInd).tickSwitch=zeros(height*width,2);




figure(figureInd);
if figureS(1)~=0
    set(gcf, 'position', [0 0 figureS(1) figureS(2)]);
elseif subplotSize(1)~=0
    canvaseSize=[0,0];
    canvaseSize(1)=subplotSize(1)*width/(xPlotRange(2)-xPlotRange(1)-(width-1)*gap);
    canvaseSize(2)=subplotSize(2)*height/(yPlotRange(2)-yPlotRange(1)-(height-1)*gap);
    
    if maxSizeSwitch==1
        canvaseSize=min(screenSize(1)/canvaseSize(1).*canvaseSize, screenSize(2)/canvaseSize(2).*canvaseSize);
    end
    set(gcf, 'position', [0 0 canvaseSize(1) canvaseSize(2)]);
end


photoInd=1;
for verticalInd=1:height
    for horizontalInd=1:width

        subPlotIndex(photoInd)=subplot('Position',[xPlotRange(1)+(xPlotRangeStep+gap)*(horizontalInd-1) yPlotRange(1)+(yPlotRangeStep+gap)*(height-verticalInd) xPlotRangeStep yPlotRangeStep]);
        set(gca,'yticklabel',[]);
        set(gca,'xticklabel',[]);
       
        if horizontalInd==1 && yticklabelSwitch==1
            Global_multiDrawFunctionSet_figure(figureInd).tickSwitch(photoInd,2)=1;
        end
        if verticalInd==1 && xticklabelSwitch==1
            Global_multiDrawFunctionSet_figure(figureInd).tickSwitch(photoInd,1)=1;
        end

        photoInd=photoInd+1;

     end

end

axes('Position',[0 0 1 1],'Visible','off');
if ~isempty(titleText)
    if ~isempty(note)
        titleYPos=0.18;
    else
        titleYPos=0.05;
    end
    text(0.50,titleYPos,titleText, 'FontUnits', 'normalized', 'FontSize',0.05, ...
        'FontWeight','bold','HorizontalAlignment','center','HorizontalAlignment','center');
end

if ~isempty(xLabelText)
    text(.5,.95,xLabelText,'FontUnits', 'normalized', 'FontSize',0.04, ...
        'HorizontalAlignment','center','HorizontalAlignment','center');   
end

if ~isempty(yLabelText)
    text(.05,.50,yLabelText,'FontUnits', 'normalized', 'FontSize',0.04, ...
        'HorizontalAlignment','center','VerticalAlignment','middle','Rotation',90);   
end

if ~isempty(note)
    text(0.50,.075,note,'FontUnits', 'normalized', 'FontSize',0.04, ...
        'HorizontalAlignment','center','VerticalAlignment','middle');   
end

Global_multiDrawFunctionSet_figure(figureInd).subPlotIndex=subPlotIndex;
